{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "import nibabel as nib\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def crop_max_roi(nii_file, mask_file, output_folder):\n",
    "    # Load the nii and mask files\n",
    "    nii_data = nib.load(nii_file).get_fdata()\n",
    "    mask_data = nib.load(mask_file).get_fdata()\n",
    "\n",
    "    # Find the maximum ROI slice\n",
    "    max_roi_slice = np.argmax(np.sum(mask_data, axis=(1, 2)))\n",
    "\n",
    "    # Crop the nii data and mask data to the maximum ROI slice\n",
    "    cropped_nii_data = nii_data[max_roi_slice, :, :]\n",
    "    cropped_mask_data = mask_data[max_roi_slice, :, :]\n",
    "\n",
    "    # Save the cropped nii data as d.nii.gz\n",
    "    cropped_nii_img = nib.Nifti1Image(cropped_nii_data, affine=None)\n",
    "    nib.save(cropped_nii_img, os.path.join(output_folder, 'd.nii.gz'))\n",
    "\n",
    "    # Save the cropped mask data as c.png\n",
    "    plt.imsave(os.path.join(output_folder, 'c.png'), cropped_mask_data, cmap='gray')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Specify the paths to the nii and mask files\n",
    "nii_file = 'D:\\\\BaiduNetdiskDownload\\\\haitao_images_gz\\\\ZS22236168\\\\DWI.nii.gz'\n",
    "mask_file = 'D:\\\\workspace\\\\2024\\\\pancreas_format\\\\mask\\\\ZS22236168+DWI.nii.gz'\n",
    "output_folder = 'output'\n",
    "\n",
    "# Call the function to crop the files\n",
    "crop_max_roi(nii_file, mask_file, output_folder)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = nib.load(mask_file).get_fdata()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "np.int64(104)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmax(np.sum(a, axis=(1, 2)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def crop_file(file_path_A, file_path_B):\n",
    "    # Implement the crop function here\n",
    "    print(f\"Cropping files: {file_path_A} and {file_path_B}\")\n",
    "\n",
    "# Path to folders A and B\n",
    "folder_A = 'path/to/folder/A'\n",
    "folder_B = 'path/to/folder/B'\n",
    "\n",
    "# Get the list of files in folders A and B\n",
    "files_A = os.listdir(folder_A)\n",
    "files_B = os.listdir(folder_B)\n",
    "\n",
    "# Find the common file names in folders A and B\n",
    "common_files = set(files_A) & set(files_B)\n",
    "\n",
    "# Process the common files\n",
    "for file_name in common_files:\n",
    "    file_path_A = os.path.join(folder_A, file_name)\n",
    "    file_path_B = os.path.join(folder_B, file_name)\n",
    "    crop_file(file_path_A, file_path_B)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
